<template>
  <div class="my-tag">
    <input
      v-if = "isEdit"
      v-focus
      @blur = "isEdit = false"
      ref="inp"
      class="input"
      type="text"
      placeholder="输入标签"
      :value="value"
      @keydown.enter = "confirm"
    />
    <div class="text" v-else @dblclick="isEdit = true">
         {{value}}
    </div>
  </div>
</template>

<script>
export default {

  data(){
      return {
        isEdit : false
      } 
  },
  // 父传子
  props: {
    value:{
      type:String,
      required: true
    }
  },
  directives:{
    focus:{
      inserted(el){
        el.focus()
      }
    }
  },
   methods: {
    confirm(e) {
      // 子传父将最新的数据交给父组件修改
      this.$emit('input', e.target.value)
      // 将输入框隐藏
      this.isEdit = false
    }
  }
}
</script>

<style lang="less" scoped>
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>